<?php

namespace app\zyhsapi\controller\v5;

use app\common\cachemodel\ZlcrAstr;
use app\common\lib\Util;
use app\zyhsapi\controller\ZyhsAuth;
use app\zyhsapi\model\good_rtc_live\ZyhsGoodRtcLiveMedia;
use app\zyhsapi\model\good_rtc_live\ZyhsGoodRtcLiveRecord;
use app\zyhsapi\model\ZyhsAppNotice;
use app\zyhsapi\model\ZyhsBanner;
use app\zyhsapi\model\ZyhsConsultQuestion;
use app\zyhsapi\service\CacheZyhs;
use think\Env;

class Index extends ZyhsAuth
{
    private $appType1 = 1; // 1=>准有好事 2=>准了APP

    /**
     * @throws \RedisException
     */
    public function index()
    {
        $banner = (new ZyhsBanner())->versionBanner();
        $bannerTmp = [];
        foreach ($banner as $value)
        {
            $bannerTmp[] = [
                'name' => $value['name'],
                'img' => $value['image'],
                'target' => $value['target'],
                'link' => $value['link'],
            ];
        }
        //登录后,注册用户赠送连麦卡券后弹出弹窗
        if ($this->user && CacheZyhs::get('reg_user_send_coupon_' . $this->user['id']))
        {
            CacheZyhs::rm('reg_user_send_coupon_' . $this->user['id']);
            $notice = (new ZyhsAppNotice())->getInfoById(1);
        } elseif ($this->user && CacheZyhs::get('reg_user_send_benefits_' . $this->user['id']))
        {
            CacheZyhs::rm('reg_user_send_benefits_' . $this->user['id']);
            $notice = (new ZyhsAppNotice())->getInfoById(2);
        } else
        {
            $notice = (new ZyhsAppNotice())->getNewestNotice();
        }
        $zoneList = config('consult_zone');
        $zoneListTmp = [
            [
                'title' => '关注',
                'type' => -1,
            ],
        ];
        foreach ($zoneList as $zone)
        {
            if ($zone['status'])
            {
                if ($zone['type'] === 5 && Util::IsHuaweiOrHonor())//华为渠道不展示婚姻关系专区
                {

                } else
                {
                    $zoneListTmp[] = [
                        'title' => $zone['name'],
                        'type' => $zone['type'],
                    ];
                }
            }
        }

        if (Util::IsHuaweiOrHonor())
        {
            $divinationQuestionLink = 'divination_question?type=0&name1=%E5%8D%A0%E6%98%9F%E9%AA%B0%E5%AD%90&name2=%E5%A1%94%E7%BD%97%E5%8D%A0%E5%8D%9C&need_auth=0';
        } else
        {
            $divinationQuestionLink = 'divination_question?type=2&name1=%E5%8D%A0%E6%98%9F%E9%AA%B0%E5%AD%90&name2=%E5%A1%94%E7%BD%97%E5%8D%A0%E5%8D%9C&need_auth=0';
        }
        $buttons = [
            [
                'name' => '平行宇宙',
                'icon' => '/zyhsapp/buttons/pxyz.png',
                'target' => 'local',
                'link' => 'community_index?type=1&need_auth=0',
            ], [
                'name' => '悬赏问题',
                'icon' => '/zyhsapp/buttons/question_answer.png',
                'target' => 'local',
                'link' => 'question_answer?need_auth=2',
            ], [
                'name' => '星图',
                'icon' => '/zyhsapp/buttons/astrolable2.png',
                'target' => 'local',
                'link' => 'astrolable?need_auth=1',
            ], [
                'name' => '合盘',
                'icon' => '/zyhsapp/buttons/composite2.png',
                'target' => 'local',
                'link' => 'composite?need_auth=1',
            ], [
                'name' => '解问题',
                'icon' => '/zyhsapp/buttons/divination_question2.png',
                'target' => 'local',
                'link' => $divinationQuestionLink,
            ], [
                'name' => '星座解读',
                'icon' => '/zyhsapp/buttons/astrolable_detail2.png',
                'target' => 'link',
                'link' => Env::get('host.domain') . "/zyhsweb/astro_explain_new/index.html?rid={$this->user['rid']}&need_auth=1",
            ],
        ];
        $zyhsGoodRtcLiveRecordModel = new ZyhsGoodRtcLiveRecord();
        $liveRecordId = $zyhsGoodRtcLiveRecordModel->getNewestRecordId(1);
        if ($liveRecordId)
        {   // 直播记录详情
            $recordInfo = $zyhsGoodRtcLiveRecordModel->getInfoById($liveRecordId);
            if ($recordInfo)
            {   // 获取 直播录音资源信息
                $mediaInfo = (new ZyhsGoodRtcLiveMedia())->getInfoById($recordInfo['current_mid']);
                $astrInfo = (new ZlcrAstr())->getInfoById($mediaInfo['astr_id'], ['avatar', 'nickname']);

                $goodRtc = [
                    'img' => '/zyhsapp/good_rtc/recommend_img.png',
                    'live_avatar' => $astrInfo['avatar'],
                    'live_name' => $astrInfo['nickname'],
                    'desc' => '播放中',
                    'user_avatar' => $mediaInfo['avatar'],
                    'user_name' => $mediaInfo['nickname'],
                    'tip_url' => '/zyhsapp/good_rtc/good_rtc_tip.png',
                    'target' => 'local',
                    'link' => 'live_official_detail_page?need_auth=0',
                ];
            }
        }
        if ($this->user)
        {   // 判断当前是否 已经 提交过了
            $zlcr3ConsultQuestionModel = new ZyhsConsultQuestion();
            $consultQuestionInfo = $zlcr3ConsultQuestionModel->getUserConsultQuestion($this->appType1, $this->user['id']);
            $popAskQuestion = $consultQuestionInfo ? 0 : 1;
        } else
        {
            $popAskQuestion = 0;
        }

        return show(config('code.success'), 'Success.', [
            'banner_list' => $bannerTmp,
            'notice' => $notice ?: null,
            'buttons' => $buttons,
            'good_rtc' => $goodRtc ?? null,
            'pop_ask_question' => $popAskQuestion,
            'selected_zone_tab_index' => 1,
            'zone_tab' => $zoneListTmp,
        ]);
    }
}
